package com.jinlejia.feedback.question.adapter.question;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.jinlejia.feedback.R;
import com.jinlejia.feedback.databinding.FbAdapterQuestionDetailsProgressBinding;
import com.jinlejia.feedback.question.ui.FbQuestionAllProgressActivity;

import java.util.ArrayList;
import java.util.Map;

//最新进度
public final class LatestProgressHolderHelper{

    static void bindViewHolder(KeyHelper.EventListener eventListener, RecyclerView.Adapter adapter, ViewHolder holder, ArrayList<Map<String, Object>> mData, int position){
        final FbAdapterQuestionDetailsProgressBinding binding = DataBindingUtil.bind(holder.itemView);
        final Map<String, Object> map = mData.get(position);
        binding.showAllInfoTextView.setVisibility(View.GONE);
        binding.infoLayout.setVisibility(View.GONE);
        binding.titleLayout.setVisibility(View.GONE);
        binding.bottomEmptyView.setVisibility(View.GONE);
        binding.simpleLayout.setVisibility(View.GONE);
        binding.lineView1.setVisibility(View.GONE);
        binding.lineView2.setVisibility(View.GONE);
        if (Boolean.parseBoolean(String.valueOf(map.get(KeyHelper.LocalIsShowAll)))){
            binding.infoLayout.setVisibility(View.VISIBLE);
            //如果没有跟进内容，则直接隐藏 不进行显示
            if ("null".equalsIgnoreCase(String.valueOf(map.get("followCode")))
                    || TextUtils.isEmpty(String.valueOf(map.get("followCode")))){ //如果没有跟进内容
                binding.followLayout.setVisibility(View.GONE);
            }else {
                binding.followLayout.setVisibility(View.VISIBLE);
                String followContent = String.valueOf(map.get("followContent"));
                followContent = "            "+followContent; //空格用来占位，以让`followFlagTextView`进行展示
                binding.noteTextDetailsView.setText(followContent);
            }
            if (map.get("taskFollowFileList")==null){ //如果没有附件下载，则不显示
                binding.fileDownloadLayout.setVisibility(View.GONE);
            }else {
                binding.fileDownloadLayout.setVisibility(View.VISIBLE);
            }
            String state = String.valueOf(map.get("state"));
            String lineOneColor = "#F4F4F4";
            String lineTwoColor = nextDataState(mData,position);

            if ("0".equals(state)){ //未进行
//                lineOneColor = "#F4F4F4";
                binding.infoStateTextView.setTextColor(Color.parseColor("#999999"));
                binding.progressIconImageView.setImageResource(R.mipmap.fb_procss_3);
            }else if ("1".equals(state)){ //进行中
//                lineOneColor = "#FCCB00";
                binding.infoStateTextView.setTextColor(Color.parseColor("#FCCB00"));
                binding.progressIconImageView.setImageResource(R.mipmap.fb_procss_2);
            }else if ("2".equals(state)){ //已完成
//                lineOneColor = "#FCCB00";
                binding.infoStateTextView.setTextColor(Color.parseColor("#999999"));
                binding.progressIconImageView.setImageResource(R.mipmap.fb_procss_1);
            }
            boolean isOverdue = (boolean) map.get("isOverdue"); //是否逾期
            if (isOverdue){ //逾期
                binding.infoTimeTextView.setTextColor(Color.parseColor("#FF5918"));
            }else { //未逾期
                binding.infoTimeTextView.setTextColor(Color.parseColor("#555555"));
            }
            //如果不是最后一个
            if (!Boolean.parseBoolean(String.valueOf(map.get(KeyHelper.LocalFollowIsLasted)))){
                binding.lineView1.setVisibility(View.VISIBLE);
                binding.lineView1.setBackgroundColor(Color.parseColor(lineOneColor));
                if (lineTwoColor==null){
                    binding.lineView2.setVisibility(View.GONE);
                }else {
                    binding.lineView2.setVisibility(View.VISIBLE);
                    binding.lineView2.setBackgroundColor(Color.parseColor(lineTwoColor));
                }
            }
        }

        if (Boolean.parseBoolean(String.valueOf(map.get(KeyHelper.LocalFollowIsLasted)))){
            if (Boolean.parseBoolean(String.valueOf(map.get(KeyHelper.LocalIsShowAll)))){
                binding.bottomEmptyView.setVisibility(View.VISIBLE);
            }else {
                binding.bottomEmptyView.setVisibility(View.GONE);
            }
            binding.lineView1.setVisibility(View.GONE);
            binding.lineView2.setVisibility(View.GONE);
        }
        //region 预期时间
        String time = String.valueOf(map.get("estimatedStartTime"));//预计开始日期
        String estimatedEndTime = String.valueOf(map.get("estimatedEndTime"));
        if (!"null".equalsIgnoreCase(estimatedEndTime) && !TextUtils.isEmpty(estimatedEndTime)){
            time = time+"至"+estimatedEndTime;
        }
        binding.infoTimeTextView.setText(time);
        //endregion

        //region 跟进信息

        //endregion
        binding.setMap(map);

        binding.lookAllCountView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                if (context!=null && context instanceof Activity){
                    Activity activity = (Activity) context;
                    Intent intent = new Intent(); //全部跟进
                    intent.setClass(context,FbQuestionAllProgressActivity.class);
                    intent.putExtra("taskCode",String.valueOf(map.get("taskCode"))); //传递任务编码
                    intent.putExtra("taskTitle",String.valueOf(map.get("taskTitle"))); //传递任务标题
                    activity.startActivity(intent);
                }
            }
        });
    }

    private static String nextDataState(ArrayList<Map<String, Object>> mData, int position){
        String lineColor = null;
        if (mData.size()>position+1){ //如果拥有下一条数据
            Map<String, Object> nextData = mData.get(position + 1);
            if ("4".equals(String.valueOf(nextData.get("type")))){ //并且还是跟进进度
                String state = String.valueOf(nextData.get("state"));
                if ("0".equals(state)){ //未进行
                    lineColor = "#F4F4F4";
                }else if ("1".equals(state)){ //进行中
                    lineColor = "#F4F4F4";
                }else if ("2".equals(state)){ //已完成
                    lineColor = "#F4F4F4";
                }
            }
        }
        return lineColor;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

}
